package org.aiforum.backend.common;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Cookie帮助类
 * @author VIC
 *
 */
public class CookieHelper {
	
	/**
	 * 获取cooke
	 * @param name
	 * @param request
	 * @return
	 */
	public static String getCookie(String name,HttpServletRequest request){
		Cookie[] cookies = request.getCookies();
		for(Cookie cookie : cookies){
			if(null != cookie && cookie.getName().equals(name)){
				if(cookie.getValue().trim().length() > 0){
					return cookie.getValue();
				}
			}
		}
		return null;
	}
	
	/**
	 * 设置Cookie
	 * @param name
	 * @param value
	 */
	public static void setCookie(String name,String value,HttpServletResponse response){
		Cookie cookie = new Cookie(name, value);
		response.addCookie(cookie);
	}

	/**
	 * 删除Cookie
	 * @param name
	 * @param response
	 */
	public static void delCookie(String name, HttpServletResponse response) {
		Cookie cookie = new Cookie(name, null);
		cookie.setMaxAge(0);
		response.addCookie(cookie);
	}

}
